package br.com.etecmatao.admin.controller;

import br.com.etecmatao.business.BusinessLocator;
import br.com.etecmatao.business.impl.BusinessLocatorImpl;
import br.com.etecmatao.model.Albuns;
import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

@ManagedBean
public class FormAlbuns {

    private Albuns albuns;
    private BusinessLocator businessLocator;
    private DataModel<Albuns> lista;

    public FormAlbuns() {
        albuns = new Albuns();
        lista = new ListDataModel<Albuns>();
        businessLocator = new BusinessLocatorImpl();
    }

    public DataModel<Albuns> getLista() {
        lista = new ListDataModel<Albuns>(businessLocator.getAlbunsBusiness().findByAll());
        return lista;
    }

    public void setLista(DataModel<Albuns> lista) {
        this.lista = lista;
    }

    public Albuns getAlbuns() {
        return albuns;
    }

    public void setAlbuns(Albuns albuns) {
        this.albuns = albuns;
    }

    public void save() {
        albuns.setLogin((br.com.etecmatao.model.Login) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user"));
        albuns.setData(new Date());
        albuns.setStatus("A");
        if(albuns.getIdAlbuns() != null)
            businessLocator.getAlbunsBusiness().update(albuns);
        else
            businessLocator.getAlbunsBusiness().save(albuns);
        FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage("Cadastro realizado com sucesso"));
    }
    public void delete() {
        albuns = lista.getRowData();
        businessLocator.getAlbunsBusiness().delete(albuns);
        FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage("Deletado com sucesso"));
    }

    public String editar() {
        albuns = lista.getRowData();
        return "formAlbuns";
    }

    public String create() {
        albuns = new Albuns();
        return "formAlbuns";
    }
}